/**
 * userOptions.js
 * Copyright (C) 2008-2011 Tommi Rautava
 * 
 * This file is part of Popmungo.
 *
 * Popmungo is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Popmungo is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

org.mozdev.popmungo.UserOptions = {
	onLoad: function userOptionsOnLoad() {
		org.mozdev.popmungo.OptionsDialog.userOptionsPaneIsLoaded = true;
		
		try {
			var charList = org.mozdev.popmungo.UserSettings.GetCharacterIdList();
			var listBox = document.getElementById("userOptions.character.list");
			
			org.mozdev.popmungo.logger.debug("characters="+ charList.length);
			
			if (charList.length > 0) {
				for (var i = 0, charId; charId = charList[i]; i++) {
					var charName = org.mozdev.popmungo.prefs.getCharacterPref(charId, 
						"extensions.popmungo.characters.<n>.name");
					
					org.mozdev.popmungo.logger.debug(charId +": "+ charName);
					
					listBox.appendItem(charName +" ("+ charId +")", charId);
				}
				
				var currentCharacter = org.mozdev.popmungo.prefs.getPref(
					"currentCharacter", 
					charList[0]) || charList[0];
				listBox.value = currentCharacter;
			}
			else {
				listBox.appendItem("");				
			}

			this.userOptionsCharacterChanged();
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError(err);
		}

		return true;
	},
	
	
	userOptionsEnableApply: function userOptionsEnableApply(enable) {
		document.getElementById("userOptions.apply.button").disabled = !enable;
		
		return true;
	},


	userOptionsApply: function userOptionsApply() {
		try {
			var listBox = document.getElementById("userOptions.character.list");
			org.mozdev.popmungo.logger.debug("listBox.value: "+ listBox.value);
			
			var charId = parseInt(listBox.value) || 0;
	
			if (charId) {
				org.mozdev.popmungo.prefs.setPref("extensions.popmungo.currentCharacter", charId);
	
				var languageId = parseInt(document.getElementById("userOptions.language.list").value);
				var dateFormat = parseInt(document.getElementById("userOptions.dateFormat.radiogroup").value);
				var timeFormat = parseInt(document.getElementById("userOptions.timeFormat.radiogroup").value);
				var cityName = document.getElementById("userOptions.city.textbox").value;
	
				org.mozdev.popmungo.prefs.setCharacterPref(
					charId, 
					"extensions.popmungo.characters.<n>.languageId", 
					languageId);
	
				org.mozdev.popmungo.prefs.setCharacterPref(
					charId, 
					"extensions.popmungo.characters.<n>.dateFormatTypeId", 
					dateFormat);
	
				org.mozdev.popmungo.prefs.setCharacterPref(
					charId, 
					"extensions.popmungo.characters.<n>.timeFormatTypeId", 
					timeFormat);
				
				org.mozdev.popmungo.prefs.setCharacterPref(
					charId, 
					"extensions.popmungo.characters.<n>.cityName", 
					cityName);				
			}
			
			this.userOptionsEnableApply(false);
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError(err);
		}
		
		return true;
	},
	
	
	userOptionsDelete: function userOptionsDelete() {
		try {
			var charId = parseInt(document.getElementById("userOptions.character.list").value || 0);
			var confirmationLabel = document.getElementById("userOptions.delete.confirmation.label");
			
			if (charId) {
				// Confirm deletion.
				if (confirm(confirmationLabel.value)) {
					// Delete prefs
					var prefBranch = "extensions.popmungo.characters." + charId + ".";
					org.mozdev.popmungo.logger.debug("Deleting preference branch "+ prefBranch);
					org.mozdev.popmungo.prefs.deleteBranch(prefBranch);
					
					var currentCharacter = org.mozdev.popmungo.prefs.getPref("currentCharacter", 0) || 0;
					
					// Reset the current character info, if the options are deleted.
					if (currentCharacter == charId) {
						org.mozdev.popmungo.logger.debug("Clearing preference"+"currentCharacter");
						org.mozdev.popmungo.prefs.clearUserPref("currentCharacter");
					}
	
					// Reload list items.
					var listBox = document.getElementById("userOptions.character.list");
					listBox.removeAllItems();
					listBox.value = "";
					this.onLoad();
				}
			}
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError(err);
		}
		
		return true;
	},
	
	
	userOptionsEnableAll: function userOptionsEnableAll(enable) {
		try {
			var disable = !enable;
			
			document.getElementById("extensions.popmungo.options.autoDetectCityName.enabled").disabled = disable;
			document.getElementById("extensions.popmungo.options.autoDetectDateAndTimeFormat.enabled").disabled = disable;
			document.getElementById("extensions.popmungo.options.autoDetectLanguage.enabled").disabled = disable;
	
			document.getElementById("userOptions.character.list").disabled = disable;
			document.getElementById("userOptions.delete.button").disabled = disable;
	
			document.getElementById("userOptions.language.list").disabled = disable;
			document.getElementById("userOptions.dateFormat.radiogroup").disabled = disable;
			document.getElementById("userOptions.timeFormat.radiogroup").disabled = disable;
			document.getElementById("userOptions.city.textbox").disabled = disable;
			
			//document.getElementById("userOptions.apply.button").disabled = disable;
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError(err);
		}
	},
	
	
	userOptionsCharacterChanged: function userOptionsCharacterChanged() {
		try {
			var charId = parseInt(document.getElementById("userOptions.character.list").value || 0);
	
			if (charId) {
				org.mozdev.popmungo.prefs.setPref("currentCharacter", charId);
				
				var portrait = org.mozdev.popmungo.prefs.getCharacterPref(charId, 
					"extensions.popmungo.characters.<n>.portraitUrl");
	
				var languageId = org.mozdev.popmungo.prefs.getCharacterPref(charId, 
					"extensions.popmungo.characters.<n>.languageId");
	
				var dateFormat = org.mozdev.popmungo.prefs.getCharacterPref(charId, 
					"extensions.popmungo.characters.<n>.dateFormatTypeId");
	
				var timeFormat = org.mozdev.popmungo.prefs.getCharacterPref(charId, 
					"extensions.popmungo.characters.<n>.timeFormatTypeId");
				
				var cityName = org.mozdev.popmungo.prefs.getCharacterPref(charId, 
					"extensions.popmungo.characters.<n>.cityName");
				
				document.getElementById("userOptions.characterPortrait.image").src = portrait;
				document.getElementById("userOptions.language.list").value = languageId;
				document.getElementById("userOptions.dateFormat.radiogroup").value = dateFormat;
				document.getElementById("userOptions.timeFormat.radiogroup").value = timeFormat;
				document.getElementById("userOptions.city.textbox").value = cityName;
				this.userOptionsEnableAll(true);
			}
			else {
				document.getElementById("userOptions.characterPortrait.image").src = "";
				document.getElementById("userOptions.language.list").value = 0;
				document.getElementById("userOptions.dateFormat.radiogroup").value = 0;
				document.getElementById("userOptions.timeFormat.radiogroup").value = 0;
				document.getElementById("userOptions.city.textbox").value = "";
				this.userOptionsEnableAll(false);
			}
			
			this.userOptionsEnableApply(false);
		}
		catch (err) {
			org.mozdev.popmungo.logger.logError(err);
		}
		
		return true;
	}
};

org.mozdev.popmungo.logger.log("org.mozdev.popmungo.UserOptions loaded");

// EOF